knitr::opts_chunk$set(echo = TRUE)

Overview

The resp_surf() function was written to solve a problem frequently encountered in psychological research testing fit-hypotheses: clustered data. Failing to address clustered data results in incorrect estimates of standard errors which causes inflated type I and type II erros when conducting hypothesis testing (Bliese & Hanges, 2004). This function automates the methodology outlined in Edwards & Parry (1993) and allows for the user to indicate a clustering variable that will be accounted for using cluster robust standard errors as implemented by the sandwich package (see ?vcovCL and Cameron et al, 2011 for more information)

The companion function plot_surf() provides quick and easy wrapper that plots objects created by the resp_surf() fucntion.

List of Arguments

resp_surf()

1) data: a data frame that contains all variables of interest. Note that quadratic terms will be created for you, although the user must center data at the appropriate values. 2) dep_var: The quoted name of your dependent variable 3) fit_var: A character vector of the two variables you are interested in examining congruence. Must be length 2. 4) control: A character vector that contains the names of control variables of interest. 5) robust: A logical value. If TRUE robust standard error should be used. If a cluster is not provided, a simple sandwich covariance is used instead 6) cluster: a character value that specifies the name of the clustering variable

plot_surf()

1) obj: an object created by resp_surf() 2) max.x: the maximum value of your first fit_var to be plotted. Order is determined by the order you entered the variables in resp_surf() 3) min.x: the minimum value of your first fit_var to be plotted. 4) max.y: the maximum value of your second fit_var to be plotted. 5) min.y: the minimum value of your second fit_var to be plotted. 6) inc: the increments it should be plotted. Smaller increments results in more grid lines. 7) phi: the vertical rotation of the graph. 8) theta: the horizontal rotation of the graph. 9) llables: Whether lines of interest should include labels (experimental) 10) ...: takes any argument that persp() can take

Example 1: resp_surf() with No Cluster

library(tidyverse)
library(sandwich) #Make sure to install and download this package. 

#Loading the functions. Works when resp_surf.R and plot_surf.R are saved in the working directory
source("resp_surf.R")
source("plot_surf.R")

#Reading in the data
precept_data<-read.csv("TCH Final Dataset.csv")

#Some of the tenure variables were entered as character - fixing the two obs and converting to integer
precept_data$yrs_nurse<-as.character(precept_data$yrs_nurse)
precept_data$yrs_nurse[c(18,20)]<-c(1.5, 20)
precept_data$yrs_nurse<-as.integer(precept_data$yrs_nurse)

# Centering Fit Variables at Scale Center ---------------------------------
#This step is still necessary. resp_surf() will not do this for you, but it will warn you if it notices that things aren't centered
precept_data<-precept_data%>%
  mutate_at(vars(DH, dhp, AH, ahp), funs(c=(.-4)))%>%
  mutate_at(vars(yrs_nurse), funs(c = .-mean(yrs_nurse, na.rm =TRUE)))

#Model 1: No clustering
irb_DH_c<-resp_surf(dep_var = "irb", fit_var = c("DH_c", "dhp_c"), control = c("yrs_nurse"),  data = precept_data)

#Table illustrating differences and congruence
irb_DH_c$dif_tab

#Results generated by selected options. When robust = FALSE results will equal lm() output
irb_DH_c$results

# Data frame containing linear and quadratic tests for lines of interest 
irb_DH_c$loi

# Calculations for stationary point
irb_DH_c$stat_pnt

# Calculations for principle axes
irb_DH_c$princ_axis

# Raw output from lm(). Used in majority of behind the scenes calculations
irb_DH_c$model

# Equation that was used to fit model
irb_DH_c$equation

Example 2: Cluster Robust Standard Errors

# Format is identical with two changes: robust = TRUE, cluster = "preceptor_name"
irb_DH_c<-resp_surf(dep_var = "irb", fit_var = c("DH_c", "dhp_c"),  data = precept_data, control = c("yrs_nurse"), robust = TRUE, cluster = "preceptor_name" )

#Standard errors have changed as the method has changed. Format of the output remains the same. 
irb_DH_c$results

resp_surf(): Plotting Examples

plot_surf(irb_DH_c, max.x = 3, min.x = -3, max.y = 3, min.y = -3, inc = 1, ticktype = "detailed")


jimmyrigby94/rrs documentation built on May 12, 2020, 3:41 p.m.